package com.yc.bank.biz;

import com.yc.bank.dao.AccountDao;
import com.yc.bank.dao.OprecordDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional(rollbackFor = BizException.class)
public class BankBiz {

    @Resource
    AccountDao adao;
    @Resource
    OprecordDao odao;

    /**
     * 默认情况下, Spring只对业务方法的 RuntimeException 执行回滚
     *                              编译期异常 Exception 不回滚
     *       noRollbackFor 设值不会滚的异常
     */
    // 存款
    public void deposit(String aid, double money) throws BizException {
        adao.updateBalanceById(aid, money);
        // int i = 1/0;
//        boolean t = true;
//        if(t){
//            throw new BizException("操作流水表已满!");
//        }
        odao.insert(aid, money);
    }

    // 取款
    @Transactional(propagation = Propagation.NEVER)
    public void withdraw(String aid, double money) {
        adao.updateBalanceById(aid, -money);
        odao.insert(aid, -money);
    }

    // 转账
    public void transfer(String aid1, String aid2, double money) throws BizException {
        withdraw(aid1, money);
        deposit(aid2, money);
    }
}
